{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Minimum Time Difference"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Medium"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #math #string #sorting"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #数学 #字符串 #排序"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: findMinDifference"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #最小时间差"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给定一个 24 小时制（小时:分钟 <strong>\"HH:MM\"</strong>）的时间列表，找出列表中任意两个时间的最小时间差并以分钟数表示。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>timePoints = [\"23:59\",\"00:00\"]\n",
    "<strong>输出：</strong>1\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>timePoints = [\"00:00\",\"23:59\",\"00:00\"]\n",
    "<strong>输出：</strong>0\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>2 &lt;= timePoints.length &lt;= 2 * 10<sup>4</sup></code></li>\n",
    "\t<li><code>timePoints[i]</code> 格式为 <strong>\"HH:MM\"</strong></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [minimum-time-difference](https://leetcode.cn/problems/minimum-time-difference/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [minimum-time-difference](https://leetcode.cn/problems/minimum-time-difference/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[\"23:59\",\"00:00\"]', '[\"00:00\",\"23:59\",\"00:00\"]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        def convert(x):\n",
    "            return int(x[:2]) * 60 + int(x[3:5])\n",
    "        ans = inf\n",
    "        timePoints.sort()\n",
    "        for x, y in pairwise(timePoints):\n",
    "            time_x = convert(x)\n",
    "            time_y = convert(y)\n",
    "            ans = min(ans, time_y - time_x)\n",
    "        ans = min(ans, 1440 + convert(timePoints[0]) - convert(timePoints[-1]))\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        d = set()\n",
    "        for c in timePoints:\n",
    "            k = int(c[: 2]) * 60 + int(c[3: ])\n",
    "            if k in d:  #可能快在了判重这里\n",
    "                return 0\n",
    "            d.add(k)\n",
    "        d = sorted(d)\n",
    "        d.append(d[0] + 1440)\n",
    "        return min(d[i] - d[i - 1] for i in range(1, len(d)))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "def getMinutes(t: str) -> int:\n",
    "    return ((ord(t[0]) - ord('0')) * 10 + ord(t[1]) - ord('0')) * 60 + (ord(t[3]) - ord('0')) * 10 + ord(t[4]) - ord('0')\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        timePoints.sort()\n",
    "        ans = float('inf')\n",
    "        t0Minutes = getMinutes(timePoints[0])\n",
    "        preMinutes = t0Minutes\n",
    "        for i in range(1, len(timePoints)):\n",
    "            minutes = getMinutes(timePoints[i])\n",
    "            ans = min(ans, minutes - preMinutes)  # 相邻时间的时间差\n",
    "            preMinutes = minutes\n",
    "        ans = min(ans, t0Minutes + 1440 - preMinutes)  # 首尾时间的时间差\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        n = len(timePoints)\n",
    "        if n > 1440:\n",
    "            return 0\n",
    "        timePoints.sort()\n",
    "        ans = float('inf')\n",
    "        t0Minutes = getMinutes(timePoints[0])\n",
    "        preMinutes = t0Minutes\n",
    "        for i in range(1, n):\n",
    "            minutes = getMinutes(timePoints[i])\n",
    "            ans = min(ans, minutes - preMinutes)  # 相邻时间的时间差\n",
    "            preMinutes = minutes\n",
    "        ans = min(ans, t0Minutes + 1440 - preMinutes)  # 首尾时间的时间差\n",
    "        return ans\n",
    "\n",
    "def getMinutes(t: str) -> int:\n",
    "    return ((ord(t[0]) - ord('0')) * 10 + ord(t[1]) - ord('0')) * 60 + (ord(t[3]) - ord('0')) * 10 + ord(t[4]) - ord('0')\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "def getMinutes(t: str) -> int:\n",
    "    return ((ord(t[0]) - ord('0')) * 10 + ord(t[1]) - ord('0')) * 60 + (ord(t[3]) - ord('0')) * 10 + ord(t[4]) - ord('0')\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        timePoints.sort()\n",
    "        ans = float('inf')\n",
    "        t0Minutes = getMinutes(timePoints[0])\n",
    "        preMinutes = t0Minutes\n",
    "        for i in range(1, len(timePoints)):\n",
    "            minutes = getMinutes(timePoints[i])\n",
    "            ans = min(ans, minutes - preMinutes)  # 相邻时间的时间差\n",
    "            preMinutes = minutes\n",
    "        ans = min(ans, t0Minutes + 1440 - preMinutes)  # 首尾时间的时间差\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        ans = 1441\n",
    "        n = len(timePoints)\n",
    "        timePoints.sort()\n",
    "        for i, j in enumerate(timePoints):\n",
    "            if not i:\n",
    "                records = (24 + int(j.split(\":\")[0]) - int(timePoints[-1].split(\":\")[0]))*60 + int(j.split(\":\")[1]) \\\n",
    "                          - int(timePoints[-1].split(\":\")[1])\n",
    "                ans = min(ans, records)\n",
    "                continue\n",
    "            records = (int(j.split(\":\")[0]) - int(timePoints[i - 1].split(\":\")[0]))*60 + \\\n",
    "                       int(j.split(\":\")[1]) - int(timePoints[i - 1].split(\":\")[1])\n",
    "            ans = min(ans, records)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "def getMinutes(t: str) -> int:\n",
    "    return ((ord(t[0]) - ord('0')) * 10 + ord(t[1]) - ord('0')) * 60 + (ord(t[3]) - ord('0')) * 10 + ord(t[4]) - ord('0')\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        timePoints.sort()\n",
    "        ans = float('inf')\n",
    "        t0Minutes = getMinutes(timePoints[0])\n",
    "        preMinutes = t0Minutes\n",
    "        for i in range(1, len(timePoints)):\n",
    "            minutes = getMinutes(timePoints[i])\n",
    "            ans = min(ans, minutes -  preMinutes)\n",
    "            preMinutes = minutes\n",
    "        ans = min(ans, t0Minutes + 1440 -preMinutes)\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        n = len(timePoints)\n",
    "        f = lambda s: int(s[:2]) * 60 + int(s[-2:])\n",
    "        times = [False] * 1440\n",
    "        mn, mx = 1440, 0\n",
    "        for x in map(f, timePoints):\n",
    "            if times[x]:\n",
    "                return 0\n",
    "            times[x] = True\n",
    "            mn = min(mn, x)\n",
    "            mx = max(mx, x)\n",
    "        ans = mn + 1440 - mx\n",
    "        for x in range(mn+1, mx+1):\n",
    "            if times[x]:\n",
    "                ans = min(ans, x - mn)\n",
    "                mn = x\n",
    "        return ans\n",
    "        \n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        for i in range(len(timePoints)):\n",
    "            item=timePoints[i]\n",
    "            timePoints[i]=int(item[:2])*60+int(item[3:])\n",
    "        timePoints.sort()\n",
    "        ret=(timePoints[0]+24*60)-timePoints[-1]\n",
    "        last=timePoints[0]\n",
    "        for i in range(1,len(timePoints)):\n",
    "            ret=min(ret,timePoints[i]-last)\n",
    "            last=timePoints[i]\n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        # consider two conditions\n",
    "        # time could be 23:59-00:01 or 24:01-23:59\n",
    "        minites=[0]*1440*2\n",
    "        # has duplicate\n",
    "        if len(timePoints)>1440:\n",
    "            return 0\n",
    "        for tp in timePoints:\n",
    "            mint=int(tp[0:2])*60+int(tp[3:])\n",
    "            # has duplicate\n",
    "            if minites[mint]!=0:\n",
    "                return 0\n",
    "            # adds cur time\n",
    "            minites[mint]+=1\n",
    "            # adds tomorrow time\n",
    "            minites[mint+1440]+=1\n",
    "        ret,last=1440,-1\n",
    "        # iterator from smaller to bigger\n",
    "        for i in range(len(minites)):\n",
    "            if minites[i]==0:\n",
    "                continue\n",
    "            elif last!=-1:\n",
    "                print(f\"i={i},last={last}\")\n",
    "                ret=min(ret,i-last)\n",
    "            last=i\n",
    "\n",
    "        return ret\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "\n",
    "        def get_minutes(t: str) -> int:\n",
    "            h_and_m = t.split(\":\")\n",
    "            h = h_and_m[0]\n",
    "            m = h_and_m[1]\n",
    "            return int(h) * 60 + int(m)\n",
    "\n",
    "\n",
    "        timePoints.sort()\n",
    "        ans = float('inf')\n",
    "        t0_minutes = get_minutes(timePoints[0])\n",
    "        pre_minutes = t0_minutes\n",
    "        for i in range(1, len(timePoints)):\n",
    "            minutes = get_minutes(timePoints[i])\n",
    "            ans = min(ans, minutes - pre_minutes)  # 相邻时间的时间差\n",
    "            pre_minutes = minutes\n",
    "\n",
    "        ans = min(ans, t0_minutes + 1440 - pre_minutes)  # 首尾时间的时间差\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from typing import *\n",
    "import math\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        n = len(timePoints)\n",
    "        if n <= 1: return 0\n",
    "        sortedTimePoints = sorted(timePoints)\n",
    "        def diff(str1, str2):\n",
    "            s = int(str1[:2]) * 60 + int(str1[3:])\n",
    "            e = int(str2[:2]) * 60 + int(str2[3:])\n",
    "            d = abs(s - e)\n",
    "            \n",
    "            return min(1440 - d, d)\n",
    "        \n",
    "        ans = diff(sortedTimePoints[0], sortedTimePoints[-1])\n",
    "        prev = sortedTimePoints[0]\n",
    "        for i in range(1, n):\n",
    "            ans = min(ans,  diff(prev, sortedTimePoints[i]))\n",
    "            prev = sortedTimePoints[i]\n",
    "        return ans\n",
    "        \n",
    "        \n",
    "# s = Solution()\n",
    "# print( s.findMinDifference([\"00:00\",\"23:55\",\"10:26\", \"10:30\"]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "TOTAL = 24 * 60\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        if len(timePoints) > TOTAL:\n",
    "            return 0\n",
    "        seats = [0] * TOTAL\n",
    "        for t in timePoints:\n",
    "            seats[int(t[:2]) * 60 + int(t[-2:])] += 1\n",
    "        first, last, ans = None, None, TOTAL\n",
    "        for i in range(TOTAL):\n",
    "            if seats[i] > 1:\n",
    "                return 0\n",
    "            elif seats[i]:\n",
    "                if first is None:\n",
    "                    first = last = i\n",
    "                else:\n",
    "                    ans = min(ans, i - last)\n",
    "                last = i\n",
    "        \n",
    "        return min(ans, first - last + TOTAL)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        def campare(time1,time2):\n",
    "            time1 = list(map(int,time1.split(\":\")))\n",
    "            time2 = list(map(int,time2.split(\":\")))\n",
    "            t1 = time1[0] * 60 + time1[1]\n",
    "            t2 = time2[0] * 60 + time2[1]\n",
    "            diff = abs(t2-t1)\n",
    "            return min(diff, Max_time-diff)\n",
    "            \n",
    "        timePoints.sort()\n",
    "        n = len(timePoints)\n",
    "        Max_time = 24*60\n",
    "        min_m = Max_time\n",
    "        for i in range(n):\n",
    "            m = campare(timePoints[i-1],timePoints[i])\n",
    "            if m < min_m:\n",
    "                min_m = m\n",
    "        return min_m\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        def convert_time_to_int(s):\n",
    "            return int(s[:2]) * 60 + int(s[3:])\n",
    "        timePoints.sort()\n",
    "        diff = convert_time_to_int(timePoints[-1]) - convert_time_to_int(timePoints[0])\n",
    "        ans = min(diff, 1440 - diff)\n",
    "        for a, b in pairwise(timePoints):\n",
    "            diff = convert_time_to_int(b) - convert_time_to_int(a)\n",
    "            ans = min(ans, min(diff, 1440 - diff))\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        for i in range(len(timePoints)):\n",
    "            h, m = map(int, timePoints[i].split(':'))\n",
    "\n",
    "            timePoints[i] = h*60 + m \n",
    "\n",
    "        timePoints.sort()\n",
    "        # timePoints.append(timePoints[0])\n",
    "\n",
    "        min_diff = float('inf')\n",
    "\n",
    "        print(timePoints)\n",
    "        for i in range(1, len(timePoints)):\n",
    "            time_diff = abs(timePoints[i] - timePoints[i-1])\n",
    "            time_diff = min(time_diff, 1440-time_diff)\n",
    "            min_diff = min(min_diff, time_diff)\n",
    "\n",
    "        time_diff = abs(timePoints[0]+1440 - timePoints[-1])\n",
    "        time_diff = min(time_diff, 1440-time_diff)\n",
    "        min_diff = min(min_diff, time_diff)\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "        return min_diff"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: list[str]) -> int:\n",
    "        def dif_24(T):\n",
    "            return abs(T2t(T) - T2t(\"24:00\"))\n",
    "        def dif_00(T):\n",
    "            return abs(T2t(T) - T2t(\"00:00\"))\n",
    "\n",
    "        def T2t(Time: str):\n",
    "            h, m = Time.split(\":\")\n",
    "            return int(h) * 60 + int(m)\n",
    "\n",
    "        if len(timePoints) != len(set(timePoints)):\n",
    "            return 0\n",
    "        timePoints = sorted(set(timePoints))\n",
    "\n",
    "\n",
    "        res =[abs(dif_00(timePoints[i]) - dif_00(timePoints[i+1])) for i in range(len(timePoints)-1)]\n",
    "        res.append(dif_00(timePoints[0]) + dif_24(timePoints[-1]))\n",
    "        return min(res)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        timePoints.sort()\n",
    "        di=[self.getTimeDifference(timePoints[i],timePoints[i+1],False) for i in range(len(timePoints)-1)]\n",
    "        di.append(self.getTimeDifference(timePoints[0],timePoints[-1],True))\n",
    "        return min(di)\n",
    "    def getTimeDifference(self,timePoints0,timePoints1,l):\n",
    "        t0,t1=timePoints0.split(':'),timePoints1.split(':')\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "        if not l:\n",
    "            hours=int(t1[0])-int(t0[0])\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "            minutes=int(t1[1])-int(t0[1])\n",
    "            return hours*60+minutes\n",
    "        else:\n",
    "\n",
    "\n",
    "\n",
    "       \n",
    "            part0=(24-int(t1[0]))*60+0-int(t1[1])\n",
    "\n",
    "            part1=int(t0[0])*60+int(t0[1])\n",
    "\n",
    "\n",
    "\n",
    "            return part0+part1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        timePoints.sort()\n",
    "        di=[self.getTimeDifference(timePoints[i],timePoints[i+1],False) for i in range(len(timePoints)-1)]\n",
    "        di.append(self.getTimeDifference(timePoints[0],timePoints[-1],True))\n",
    "        return min(di)\n",
    "    def getTimeDifference(self,timePoints0,timePoints1,l):\n",
    "        t0,t1=timePoints0.split(':'),timePoints1.split(':')\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "        if not l:\n",
    "            hours=int(t1[0])-int(t0[0])\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "            minutes=int(t1[1])-int(t0[1])\n",
    "            return hours*60+minutes\n",
    "        else:\n",
    "\n",
    "\n",
    "\n",
    "       \n",
    "            part0=(24-int(t1[0]))*60+0-int(t1[1])\n",
    "\n",
    "            part1=int(t0[0])*60+int(t0[1])\n",
    "\n",
    "\n",
    "\n",
    "            return part0+part1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        timePoints.sort()\n",
    "        res=10000\n",
    "        for i in range(len(timePoints)-1):\n",
    "            h1,m1=timePoints[i+1].split(':')\n",
    "            h2,m2=timePoints[i].split(':')\n",
    "            diff= 60*(int(h1)-int(h2)) + int(m1)-int(m2)\n",
    "            print(h1,h2,m1,m2)\n",
    "            res=min(res,diff)\n",
    "        h1,m1=timePoints[-1].split(':')\n",
    "        h2,m2=timePoints[0].split(':')\n",
    "        \n",
    "        diff= 24*60-60*(int(h1)-int(h2)) - int(m1)+int(m2)\n",
    "        res=min(res,diff)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        a=sorted(timePoints)\n",
    "        a.append(a[0])\n",
    "        b=10000\n",
    "        for v in range(len(a)-1):\n",
    "            p=int(a[v+1][0:2])-int(a[v][0:2])\n",
    "            q=int(a[v+1][3:5])-int(a[v][3:5])\n",
    "            p=p*60+q\n",
    "            if p>720:\n",
    "                p=1440-p\n",
    "            elif p<0:\n",
    "                p=1440+p \n",
    "            if p<b:\n",
    "                b=p \n",
    "        return b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        timePoints.sort()\n",
    "        di=[self.getTimeDifference(timePoints[i],timePoints[i+1],False) for i in range(len(timePoints)-1)]\n",
    "        di.append(self.getTimeDifference(timePoints[0],timePoints[-1],True))\n",
    "        return min(di)\n",
    "    def getTimeDifference(self,timePoints0,timePoints1,l):\n",
    "        t0,t1=timePoints0.split(':'),timePoints1.split(':')\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "        if not l:\n",
    "            hours=int(t1[0])-int(t0[0])\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "            minutes=int(t1[1])-int(t0[1])\n",
    "            return hours*60+minutes\n",
    "        else:\n",
    "\n",
    "\n",
    "\n",
    "       \n",
    "            part0=(24-int(t1[0]))*60+0-int(t1[1])\n",
    "\n",
    "            part1=int(t0[0])*60+int(t0[1])\n",
    "\n",
    "\n",
    "\n",
    "            return part0+part1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        timePoints.sort()\n",
    "        di=[self.getTimeDifference(timePoints[i],timePoints[i+1],False) for i in range(len(timePoints)-1)]\n",
    "        di.append(self.getTimeDifference(timePoints[0],timePoints[-1],True))\n",
    "        return min(di)\n",
    "    def getTimeDifference(self,timePoints0,timePoints1,l):\n",
    "        t0,t1=timePoints0.split(':'),timePoints1.split(':')\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "        if not l:\n",
    "            hours=int(t1[0])-int(t0[0])\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "            minutes=int(t1[1])-int(t0[1])\n",
    "            return hours*60+minutes\n",
    "        else:\n",
    "\n",
    "\n",
    "\n",
    "       \n",
    "            part0=(24-int(t1[0]))*60+0-int(t1[1])\n",
    "\n",
    "            part1=int(t0[0])*60+int(t0[1])\n",
    "\n",
    "\n",
    "\n",
    "            return part0+part1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        maxtime=24*60\n",
    "        for i in range(len(timePoints)):\n",
    "            timePoints[i]=list(map(int,timePoints[i].split(':')))\n",
    "            timePoints[i]=timePoints[i][0]*60+timePoints[i][1]\n",
    "        timePoints=sorted(timePoints)\n",
    "        print(timePoints)\n",
    "        res=[0]*(len(timePoints)-1)\n",
    "        for i in range(len(timePoints)-1):\n",
    "            res[i]=timePoints[i+1]-timePoints[i]\n",
    "        print(res)\n",
    "        res.append(24*60+timePoints[0]-timePoints[-1])\n",
    "        print(res)\n",
    "        return min(res)\n",
    "            \n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        \n",
    "        for i in range(len(timePoints)):\n",
    "            timePoints[i] = int(timePoints[i].split(':')[0]) * 60 + int(timePoints[i].split(':')[1])\n",
    "        timePoints.sort()\n",
    "        print(timePoints)\n",
    "        minDiff = float('inf')\n",
    "        first_last_diff = timePoints[0] - timePoints[-1] + 1440\n",
    "        for i in range(1,len(timePoints)):\n",
    "            if timePoints[i] - timePoints[i-1] < minDiff:\n",
    "                minDiff = timePoints[i] - timePoints[i-1]\n",
    "        return min(minDiff, first_last_diff)\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\r\n",
    "        # 1. 暴力法\r\n",
    "        # 2. 排序\r\n",
    "        # 3. 两两比较\r\n",
    "        # 4. 注意首尾时间的比较\r\n",
    "        timePoints.sort()\r\n",
    "        min_diff = 24 * 60\r\n",
    "        for i in range(len(timePoints)):\r\n",
    "            timePoints[i] = int(timePoints[i][:2]) * 60 + int(timePoints[i][3:])\r\n",
    "        for i in range(len(timePoints) - 1):\r\n",
    "            min_diff = min(min_diff, (timePoints[i + 1] - timePoints[i]))\r\n",
    "        min_diff = min(min_diff, (timePoints[0] + 24 * 60 - timePoints[-1]))\r\n",
    "        return min_diff"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        nums = []\n",
    "        for i in timePoints:\n",
    "            l = i.split(':')\n",
    "            t = int(l[0]) * 60 + int(l[1])\n",
    "            nums.append(t)\n",
    "        n = len(nums)\n",
    "        nums.sort()\n",
    "        a = min(nums[i + 1] - nums[i] for i in range(n - 1))\n",
    "        b = 1440 - nums[-1] + nums[0]\n",
    "        return min(a, b)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        '''\n",
    "        暴力破解\n",
    "        1.先全转化为分钟数，再排序\n",
    "        2.使用双指针法遍历计算相邻两个之间的时间差，不断更新最小值\n",
    "        3.返回记录的最小值\n",
    "        :param timePoints:\n",
    "        :return:\n",
    "        '''\n",
    "        n = len(timePoints)\n",
    "        minute_list = []\n",
    "        # 将时间转化为分钟数\n",
    "        for t in range(n):\n",
    "            hour, minute = timePoints[t].split(':')\n",
    "            minute_list.append(int(hour) * 60 + int(minute))\n",
    "        minute_list.sort()  # 排序\n",
    "        res = 2000\n",
    "        for left in range(n):\n",
    "            right = 0 if left == n - 1 else left + 1  # 注意23:59和00:00\n",
    "            times = abs(minute_list[right] - minute_list[left])\n",
    "            min_times = min(times, 1440 - times)\n",
    "            res = min(res, min_times)\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        res = []\n",
    "        min_res = inf\n",
    "        for i in range(len(timePoints)):\n",
    "            res.append(int(timePoints[i][:2])*60+int(timePoints[i][3:]))\n",
    "        res.sort()\n",
    "        res.append(res[0]+ 24*60)\n",
    "        for i in range(len(res)-1):\n",
    "            min_res = min(min_res,res[i+1]-res[i])\n",
    "        return min_res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "\n",
    "        def time_to_minute(s):\n",
    "            return int(s[:2]) * 60 + int(s[3:])\n",
    "\n",
    "        \n",
    "        minute = [time_to_minute(time) for time in timePoints]\n",
    "        minute.sort()\n",
    "\n",
    "        res = float('inf')\n",
    "\n",
    "        for i in range(len(minute) - 1):\n",
    "            res = min(res, minute[i+1] - minute[i])\n",
    "\n",
    "        res = min(res, 1440 + minute[0] - minute[-1])\n",
    "\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        times=[]\n",
    "        for i in timePoints:\n",
    "            l=i.split(':')\n",
    "            t=int(l[0])*60+int(l[1])\n",
    "            times.append(t)\n",
    "        times.sort()\n",
    "        a=min(times[i+1]-times[i] for i in range(len(times)-1))\n",
    "        b=1440-times[-1]+times[0]\n",
    "        return min(a,b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        time_list = []\n",
    "        for i in timePoints:\n",
    "            tmp = i.split(\":\")\n",
    "            time_list.append(int(tmp[0]) * 60 + int(tmp[1]))\n",
    "        time_list.sort()\n",
    "\n",
    "        res = time_list[0] + 1440 - time_list[-1]\n",
    "        for i in range(1, len(time_list)):\n",
    "            res = min(res, time_list[i] - time_list[i - 1])\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        def getTime(s):\n",
    "            [hour, minute] = [int(e) for e in s.split(\":\")]\n",
    "            return hour * 60 + minute\n",
    "        timePoints.sort()\n",
    "        print(timePoints)\n",
    "        n = len(timePoints)\n",
    "        ans = 24 * 60\n",
    "\n",
    "        for i in range(1, n):\n",
    "            ans = min(ans, getTime(timePoints[i]) - getTime(timePoints[i-1]))\n",
    "        ans = min(ans, 24 * 60 + getTime(timePoints[0]) - getTime(timePoints[-1]))\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        k=[0]*len(timePoints)\n",
    "        ans=2*(10**4)\n",
    "        for i in range (len(timePoints)):\n",
    "            k[i]=((int(timePoints[i][0])*10+int(timePoints[i][1]))*60+int(timePoints[i][3])*10+int(timePoints[i][4]))\n",
    "        k.sort()\n",
    "        for i in range (len(k)-1):\n",
    "            ans=min(ans,abs(k[i]-k[i+1]))\n",
    "            if i==len(k)-2:\n",
    "                ans=min(ans,abs(k[i]-k[i+1]+1440))\n",
    "        ans=min(ans,abs(k[len(k)-1]-k[0]-1440))\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        n = len(timePoints)\n",
    "        time = [0] * n\n",
    "        for i, t in enumerate(timePoints):\n",
    "            time[i] = int(t[:2]) * 60 + int(t[3:])\n",
    "        \n",
    "        time.sort()\n",
    "\n",
    "        print(time)\n",
    "\n",
    "        ans = inf\n",
    "        for i in range(n):\n",
    "            temp = min(abs(time[i] - time[i-1]), 1440 - abs(time[i] - time[i-1]))\n",
    "            ans = ans if ans <temp else temp\n",
    "        \n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        lst, ret = [], float('inf')\n",
    "        for ch in timePoints:\n",
    "            tmplst = ch.split(':')\n",
    "            lst.append(int(tmplst[0]) * 60 + int(tmplst[1]))\n",
    "        lst.sort()\n",
    "        lst.append(lst[0] + 24 * 60)\n",
    "        for i in range(len(lst) - 1):\n",
    "            ret = min(ret, lst[i + 1] - lst[i])\n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        lst, ret = [], float('inf')\n",
    "        for ch in timePoints:\n",
    "            tmplst = ch.split(':')\n",
    "            lst.append(int(tmplst[0]) * 60 + int(tmplst[1]))\n",
    "        lst.sort()\n",
    "        # 时间是一个环，需在列表尾加一个lst[0] + 24 * 60，实现首尾比较\n",
    "        lst.append(lst[0] + 24 * 60)\n",
    "        for i in range(len(lst) - 1):\n",
    "            ret = min(ret, lst[i + 1] - lst[i])\n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        minutes = []\n",
    "        for i in timePoints:\n",
    "            minutes.append(int(i.split(':')[0])*60+int(i.split(':')[1]))\n",
    "        \n",
    "        minutes.sort()\n",
    "\n",
    "        res = 721\n",
    "        if minutes[-1] - minutes[0] >720:\n",
    "            d = minutes[0]+1440 - minutes[-1]\n",
    "        else:\n",
    "            d = minutes[-1] - minutes[0]\n",
    "        \n",
    "        if d < res:\n",
    "            res = d\n",
    "        \n",
    "        for i in range(len(minutes)-1):\n",
    "            if minutes[i+1] - minutes[i] > 720:\n",
    "                d = minutes[i]+1440 - minutes[i+1]\n",
    "            else:\n",
    "                d = minutes[i+1] - minutes[i]\n",
    "            if d< res:\n",
    "                res = d \n",
    "        \n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        nums = []\n",
    "        for i in timePoints:\n",
    "            h, m = i.split(':')\n",
    "            nums.append(int(h) * 60 + int(m))\n",
    "        nums.sort()\n",
    "        return min(min(nums[i + 1] - nums[i] for i in range(len(timePoints) - 1)), (24 * 60 - nums[-1]) + nums[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        # 排序\n",
    "        lst, ret = [], float('inf')\n",
    "        for ch in timePoints:\n",
    "            tmplst = ch.split(':')\n",
    "            lst.append(int(tmplst[0]) * 60 + int(tmplst[1]))\n",
    "        lst.sort()\n",
    "        # 时间是一个环，需在列表尾加一个lst[0] + 24 * 60，实现首尾比较\n",
    "        lst.append(lst[0] + 24 * 60)\n",
    "        for i in range(len(lst) - 1):\n",
    "            ret = min(ret, lst[i + 1] - lst[i])\n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        nums = list()\n",
    "        for t in timePoints:\n",
    "            h = int(t[:2])\n",
    "            m = int(t[3:5])\n",
    "            x = h * 60 + m\n",
    "            nums.append(x)\n",
    "            #nums.append(x + 1440)\n",
    "        \n",
    "        nums.sort()\n",
    "        n = len(nums)\n",
    "        res = 3000\n",
    "        for i in range(1, n):\n",
    "            x = nums[i - 1]\n",
    "            y = nums[i]\n",
    "            res = min(res, y - x,1440-y+x)\n",
    "        res=min(res,1440- nums[-1]+ nums[0])\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        tmp = []\n",
    "        for time in timePoints:\n",
    "            x1 = time.split(':')\n",
    "            s = int(x1[0])*60 + int(x1[1])\n",
    "            tmp.append(s)\n",
    "        tmp.sort()\n",
    "        ans = 99999\n",
    "        for i in range(len(tmp)-1):\n",
    "            ans = min(ans, tmp[i+1]-tmp[i])\n",
    "        ans = min(ans, tmp[0] + 1440 - tmp[-1])\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        timePoints = [int((tmp:= time.split(\":\"))[0])  * 60 + int(tmp[1]) for time in timePoints]\n",
    "        timePoints.sort()\n",
    "        res = float(\"inf\")\n",
    "        for i in range(1, len(timePoints)):\n",
    "            res = min(res, timePoints[i] - timePoints[i - 1])\n",
    "        return min(res, timePoints[0] + 1440 - timePoints[-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        timePoints = sorted(timePoints)\n",
    "        minDist = 24 * 60\n",
    "        for i in range(len(timePoints)):\n",
    "            timePoints[i] = int(timePoints[i][:2]) * 60 + int(timePoints[i][3:])\n",
    "            if i != 0:\n",
    "                dist = timePoints[i] - timePoints[i - 1]\n",
    "                dist = 12 * 60 - abs(12 * 60 - abs(dist))\n",
    "                minDist = min(minDist, dist)\n",
    "        minDist = min(minDist, 12 * 60 - abs(12 * 60 - abs(timePoints[0] - timePoints[-1])))\n",
    "                \n",
    "        return minDist\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        minutes = [int(time[:2]) * 60 + int(time[3:]) for time in timePoints]\n",
    "        minutes.sort()\n",
    "\n",
    "        return min((y - x) % 1440 for x, y in zip(minutes, minutes[1:] + minutes[:1]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        if len(timePoints) > len(set(timePoints)):\n",
    "            print(0)\n",
    "\n",
    "        times = []\n",
    "        for i in range(len(timePoints)):\n",
    "            hour = int(timePoints[i][0:2])\n",
    "            minute = int(timePoints[i][3:5])\n",
    "            times.append(hour * 60 + minute)\n",
    "        times.sort()\n",
    "        print(times)\n",
    "        res = 1440\n",
    "        for i in range(1, len(times)):\n",
    "            res = min(res, times[i] - times[i-1])\n",
    "        res = min(res, 1440 - times[-1] + times[0])\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "\n",
    "        # 思路是想的将其转换为一个数组，之后进行排序在相减。\n",
    "        lens  = len(timePoints)\n",
    "        times_value = []\n",
    "        for i in range(lens):\n",
    "            times_value.append(int(str(timePoints[i][0:2])) * 60 + int(str(timePoints[i][3:])))\n",
    "        \n",
    "        times_value = sorted(times_value)\n",
    "\n",
    "        cha = 24 * 60\n",
    "        val = 24 * 60\n",
    "        for i in range(1,lens):\n",
    "            cha = min(cha, times_value[i] - times_value[i-1], val - (times_value[i] - times_value[i-1]))\n",
    "            print(cha, times_value[i] - times_value[i-1], val - (times_value[i] - times_value[i-1]))\n",
    "        \n",
    "        cha = min(cha, times_value[-1] - times_value[0], val - (times_value[-1] - times_value[0]))\n",
    "        return cha\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        def convertTime(str1):\n",
    "            return (int)(str1[:2]) * 60 + (int)(str1[-2:])\n",
    "\n",
    "        array = []\n",
    "        for item in timePoints:\n",
    "            array.append(convertTime(item))\n",
    "\n",
    "        array = sorted(array)\n",
    "        min1 = 1439\n",
    "        for i in range(0,len(array)):\n",
    "            print(i)\n",
    "            print(array[i] ,array[i-1])\n",
    "            min1 = min(min1, (array[i] - array[i-1] + 1440) % 1440, (-array[i-1] + array[i] + 1440) % 1440 )\n",
    "        return min1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        minutes = [int(time[:2]) * 60 + int(time[3:]) for time in timePoints]\n",
    "        minutes.sort()\n",
    "\n",
    "        return min((y - x) % 1440 for x, y in zip(minutes, minutes[1:] + minutes[:1]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "\n",
    "        # 思路是想的将其转换为一个数组，之后进行排序在相减。\n",
    "        lens  = len(timePoints)\n",
    "        times_value = []\n",
    "        for i in range(lens):\n",
    "            times_value.append(int(str(timePoints[i][0:2])) * 60 + int(str(timePoints[i][3:])))\n",
    "        \n",
    "        times_value = sorted(times_value)\n",
    "\n",
    "        cha = 24 * 60\n",
    "        val = 24 * 60\n",
    "        for i in range(1,lens):\n",
    "            cha = min(cha, times_value[i] - times_value[i-1], val - (times_value[i] - times_value[i-1]))\n",
    "            print(cha, times_value[i] - times_value[i-1], val - (times_value[i] - times_value[i-1]))\n",
    "        \n",
    "        cha = min(cha, times_value[-1] - times_value[0], val - (times_value[-1] - times_value[0]))\n",
    "        return cha\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        times=[(int(x[:2]),int(x[3:])) for x in timePoints]\n",
    "        times.sort(key=lambda x:(x[0],x[1]))\n",
    "        res=0\n",
    "        h_diff=0\n",
    "        m_diff=0\n",
    "        if times[0][1]>times[-1][1]:\n",
    "            h_diff=24+times[0][0]-times[-1][0]\n",
    "            m_diff=times[0][1]-times[-1][1]\n",
    "        else:\n",
    "            h_diff=24+times[0][0]-times[-1][0]-1\n",
    "            m_diff=60-times[-1][1]+times[0][1]\n",
    "        res=h_diff*60+m_diff\n",
    "        for i in range(1,len(times)):\n",
    "            if times[i][1]>=times[i-1][1]:\n",
    "                h_diff=times[i][0]-times[i-1][0]\n",
    "                m_diff=times[i][1]-times[i-1][1]\n",
    "            else:\n",
    "                h_diff=times[i][0]-times[i-1][0]-1\n",
    "                m_diff=60-times[i-1][1]+times[i][1]\n",
    "            res=min(res,60*h_diff+m_diff)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        mlst = []\n",
    "        for i in timePoints:\n",
    "            mlst.append(int(i.split(':')[0])*60+int(i.split(':')[1]))\n",
    "        mlst.sort()\n",
    "        mlst += [mlst[0]]\n",
    "        dlst = []\n",
    "        for i in range(len(timePoints)):\n",
    "            dif = abs(int(mlst[i+1])-int(mlst[i]))\n",
    "            dlst.append(min(dif,24*60-dif))\n",
    "        return min(dlst)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        time = [int(_[:2])*60 + int(_[-2:]) for _ in timePoints]\n",
    "        time.sort()\n",
    "        diff = 1440 + time[0] - time[-1]\n",
    "        for i, j in zip(time[:-1], time[1:]):\n",
    "            diff = min(diff , j - i)\n",
    "        return diff"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        timePoints = [list(map(int,s.split(\":\"))) for s in timePoints]\n",
    "        timePoints = sorted(timePoints)\n",
    "        past_val = timePoints[-1][0]*60+timePoints[-1][1]\n",
    "        first_val  = timePoints[0][0]*60+timePoints[0][1]\n",
    "        min_val = min(past_val - first_val,abs(24*60 - past_val + first_val))\n",
    "        past_val = first_val\n",
    "        for h,m in timePoints[1:]:\n",
    "            val = h*60+m\n",
    "            min_val = min(min_val,val - past_val)\n",
    "            past_val = val\n",
    "        return min_val"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        mylist = []\n",
    "        for item in timePoints:\n",
    "            a,b = item.split(':')\n",
    "            mylist.append((int(a),int(b)))\n",
    "        \n",
    "        mylist.sort(key = lambda x: (x[0],x[1]))\n",
    "        # print(mylist)\n",
    "\n",
    "        ans = 100000\n",
    "        for i in range(len(timePoints) - 1):\n",
    "            left = mylist[i]\n",
    "            right = mylist[i+1]\n",
    "\n",
    "            diff = (right[0] - left[0])*60 + (right[1] - left[1])\n",
    "            diff = min(diff,(1440 - diff))\n",
    "\n",
    "            if diff < ans:\n",
    "                ans = diff \n",
    "\n",
    "        right = mylist[len(timePoints)-1]\n",
    "        left = mylist[0]\n",
    "        diff = (24 - right[0] + left[0])*60 + (left[1] - right[1])\n",
    "\n",
    "        if diff < ans:\n",
    "            ans = diff         \n",
    "\n",
    "        return ans\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        timePoints = [[int(val) for val in time.split(\":\")] for time in timePoints]\n",
    "        timePoints = [cell[0] * 60 + cell[1] for cell in timePoints]\n",
    "        timePoints = list(sorted(timePoints))\n",
    "        print(timePoints)\n",
    "        res = min(timePoints[-1] - timePoints[0], 24 * 60 - timePoints[-1] + timePoints[0]) \n",
    "        for i in range(1, len(timePoints)):\n",
    "            res = min(res, timePoints[i] - timePoints[i - 1], 24 * 60 - timePoints[i] + timePoints[i - 1])\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import functools\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        length = len(timePoints)\n",
    "        def mycompare(a,b):\n",
    "           [t1,t2] = a.split(':')\n",
    "           [t3,t4] = b.split(':')\n",
    "           return int(t1)*60+int(t2) - (int(t3)*60 + int(t4))\n",
    "        def marcompare(a,b):\n",
    "           [t1,t2] = a.split(':')\n",
    "           [t3,t4] = b.split(':')\n",
    "           return int(t1)*60 + int(t2) + (24 - int(t3))*60 - int(t4)\n",
    "\n",
    "        tmp  = timePoints[:]\n",
    "        tmp.sort(key = functools.cmp_to_key(mycompare))\n",
    "        mint = 1440\n",
    "        for i in range(length-1):\n",
    "            mint = min(mint,mycompare(tmp[i+1],tmp[i]))\n",
    "        mint = min(mint,marcompare(tmp[0],tmp[-1]))\n",
    "        return mint\n",
    "\n",
    "            \n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        times = [(int(x[:2]), int(x[3:])) for x in timePoints]\n",
    "        times.sort()\n",
    "        ans = inf\n",
    "        for i in range(1, len(times)):\n",
    "            a, b = times[i - 1]\n",
    "            c, d = times[i]\n",
    "            x = 60 * (c - a) + d - b\n",
    "            ans = min(ans, x if x <= 720 else 1440 - x)\n",
    "        x = 60 * (times[-1][0] - times[0][0]) + times[-1][1] - times[0][1]\n",
    "        ans = min(ans, x if x <= 720 else 1440 - x)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        re = []\n",
    "        for t in timePoints:\n",
    "            s = t.split(\":\")\n",
    "            re.append(int(s[0])*60 + int(s[1]))\n",
    "            re.append(int(s[0])*60 + int(s[1]) + 24*60)\n",
    "        re.sort()\n",
    "        mx = 24 * 60\n",
    "        for i in range(len(re)-1):\n",
    "            mx = min(re[i+1] - re[i],mx)\n",
    "        return mx\n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        hour=[]\n",
    "        for i in range(len(timePoints)):\n",
    "            m=[int(timePoints[i][0:2]),int(timePoints[i][3:])]\n",
    "            hour.append(m)\n",
    "        #print(hour)\n",
    "        hour.sort(key=lambda x:(x[0],x[1]))\n",
    "        #print(hour)\n",
    "\n",
    "        lastone=hour[0][0]*60+hour[0][1]+(23-hour[-1][0])*60+(60-hour[-1][1])\n",
    "        for i in range(len(timePoints)-1):\n",
    "            lastone=min(lastone,60*(hour[i+1][0]-hour[i][0])+(hour[i+1][1]-hour[i][1]))\n",
    "        return lastone"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        re = []\n",
    "        for t in timePoints:\n",
    "            s = t.split(\":\")\n",
    "            re.append(int(s[0])*60 + int(s[1]))\n",
    "            re.append(int(s[0])*60 + int(s[1]) + 24*60)\n",
    "        re.sort()\n",
    "        mx = 24 * 60\n",
    "        for i in range(len(re)-1):\n",
    "            mx = min(abs(re[i+1] - re[i]),mx)\n",
    "        return mx\n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        re = []\n",
    "        for t in timePoints:\n",
    "            s = t.split(\":\")\n",
    "            re.append(int(s[0])*60 + int(s[1]))\n",
    "            re.append(int(s[0])*60 + int(s[1]) + 24*60)\n",
    "        re.sort()\n",
    "        mx = 24 * 60\n",
    "        for i in range(len(re)-1):\n",
    "            mx = min(abs(re[i+1] - re[i]),abs(24 * 60-re[i+1]+re[i]),mx)\n",
    "        return mx\n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        # 将每个值都转换成两个分钟，一个直接转换，一个加24小时就是24*60，然后两两计算\n",
    "        minutes = []\n",
    "        for t in timePoints:\n",
    "            hours, minu = t.split(':')\n",
    "            times = int(hours)*60+int(minu)\n",
    "            minutes.append(times)\n",
    "            minutes.append(times+24*60)\n",
    "        minutes.sort()\n",
    "        res = 24*60\n",
    "        for i in range(len(minutes)-1):\n",
    "            res = min(res,minutes[i+1]-minutes[i])\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import functools\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        length = len(timePoints)\n",
    "        def mycompare(a,b):\n",
    "           [t1,t2] = a.split(':')\n",
    "           [t3,t4] = b.split(':')\n",
    "           return int(t1)*60+int(t2) - (int(t3)*60 + int(t4))\n",
    "        def marcompare(a,b):\n",
    "           [t1,t2] = a.split(':')\n",
    "           [t3,t4] = b.split(':')\n",
    "           return int(t1)*60 + int(t2) + (24 - int(t3))*60 - int(t4)\n",
    "\n",
    "        tmp  = timePoints[:]\n",
    "        tmp.sort(key = functools.cmp_to_key(mycompare))\n",
    "        mint = 1440\n",
    "        for i in range(length-1):\n",
    "            mint = min(mint,mycompare(tmp[i+1],tmp[i]))\n",
    "        mint = min(mint,marcompare(tmp[0],tmp[-1]))\n",
    "        return mint\n",
    "\n",
    "            \n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        def tominute(s):\n",
    "            row = s.split(\":\")\n",
    "            return int(row[0]) * 60 + int(row[1])\n",
    "        arr = []\n",
    "        for ts in timePoints:\n",
    "            if ts == \"00:00\":\n",
    "                arr.append(0)\n",
    "                arr.append(1440)\n",
    "            else:\n",
    "                minutes = tominute(ts)\n",
    "                arr.append(minutes)\n",
    "                \n",
    "                arr.append(minutes + 1440)\n",
    "                \n",
    "        arr.sort()\n",
    "        ans = 1440\n",
    "        for i in range(len(arr)-1):\n",
    "            ans = min(ans, abs(arr[i+1]-arr[i]))\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        n = len(timePoints)\n",
    "\n",
    "        timePoints = [ float(i.split(':')[0]) * 60 + float(i.split(':')[1]) for i in timePoints ]\n",
    "        timePoints.sort()\n",
    "\n",
    "        diff = [timePoints[i+1] - timePoints[i] for i in range(n-1)]\n",
    "\n",
    "        res = min([\n",
    "            min(diff),\n",
    "            timePoints[0]+1440 - timePoints[-1]\n",
    "        ])\n",
    "        \n",
    "        return(int(res))\n",
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        n = len(timePoints)\n",
    "        dp = [0] * (2 * n)\n",
    "        index = 0\n",
    "        for i in range(n):\n",
    "            t = timePoints[i]\n",
    "            m = int(t[:2]) * 60 + int(t[-2:])\n",
    "            dp[index] = m\n",
    "            dp[index + 1] =m + 60 * 24\n",
    "            index += 2\n",
    "        dp.sort()\n",
    "        return min(dp[i + 1] - dp[i] for i in range(2 * n - 1))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        n = len(timePoints) * 2\n",
    "        nums, idx = [0] * n, 0\n",
    "        for time in timePoints:\n",
    "            h, m = int(time[:2]), int(time[-2:])\n",
    "            nums[idx] = 60 * h + m\n",
    "            nums[idx + 1] = nums[idx] + 1440\n",
    "            idx+=2\n",
    "        nums.sort()\n",
    "        return min(nums[i+1] - nums[i] for i in range(n-1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "         n = len(timePoints)\n",
    "         times = []\n",
    "         def trans(time: str):\n",
    "             hour = int(time[0:2])\n",
    "             minute = int(time[3:])\n",
    "             return hour * 60 + minute\n",
    "         res = 24 * 60\n",
    "\n",
    "         for v in timePoints:\n",
    "             times.append(trans(v))\n",
    "             times.append(trans(v) + res)\n",
    "         times.sort()\n",
    "         for i in range(1, len(times)):\n",
    "             res = min(res, times[i] - times[i - 1])\n",
    "         return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        n = len(timePoints) * 2\n",
    "        nums, idx = [0] * n, 0\n",
    "        for time in timePoints:\n",
    "            h, m = int(time[:2]), int(time[-2:])\n",
    "            nums[idx] = h * 60 + m\n",
    "            nums[idx + 1] = nums[idx] + 1440\n",
    "            idx += 2\n",
    "        nums.sort()\n",
    "        return min(nums[i + 1] - nums[i] for i in range(n - 1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        # time O(nlogn), space O(n)\n",
    "        n = len(timePoints) * 2\n",
    "        nums, idx = [0] * n, 0\n",
    "        for tp in timePoints: # 对于每个时间点，我们不仅记录「当天该时间点」对应的的偏移量，还记录「下一天该时间点」对应的偏移量\n",
    "            seconds = int(tp[:2]) * 60 + int(tp[3:])\n",
    "            nums[idx] = seconds\n",
    "            nums[idx + 1] = seconds + 1440\n",
    "            idx += 2\n",
    "        nums.sort()\n",
    "        return min(nums[i + 1] - nums[i] for i in range(n - 1))\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        DAY = 24 * 60\n",
    "        def convert(str) -> int:\n",
    "           minCnt = 0\n",
    "           minCnt += (ord(str[0]) - ord('0')) * 10 * 60\n",
    "           minCnt += (ord(str[1]) - ord('0')) * 60\n",
    "           minCnt += (ord(str[3]) - ord('0')) * 10\n",
    "           minCnt += (ord(str[4]) - ord('0'))\n",
    "           return minCnt\n",
    "\n",
    "        timePointsMin = []\n",
    "        for tp in timePoints:\n",
    "            mins = convert(tp)\n",
    "            timePointsMin.append(mins)\n",
    "            timePointsMin.append(mins + DAY)\n",
    "\n",
    "        timePointsMin = sorted(timePointsMin)       \n",
    "        \n",
    "        minDiff = DAY + 1\n",
    "        for i in range(len(timePointsMin)-1):\n",
    "            diff = timePointsMin[i+1] - timePointsMin[i]\n",
    "            if diff < minDiff:\n",
    "                minDiff = diff\n",
    "        return minDiff"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        n = len(timePoints) * 2\n",
    "        nums, idx = [0]*n, 0\n",
    "        for tp in timePoints:\n",
    "            hous, mins = int(tp[:2]), int(tp[-2:])\n",
    "            nums[idx] = hous*60 + mins\n",
    "            nums[idx + 1] = nums[idx] + 1440\n",
    "            idx += 2\n",
    "        nums.sort()\n",
    "        return min(nums[i + 1] - nums[i] for i in range(n - 1))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        DAY = 24 * 60\n",
    "        def convert(str) -> int:\n",
    "           minCnt = 0\n",
    "           minCnt += (ord(str[0]) - ord('0')) * 10 * 60\n",
    "           minCnt += (ord(str[1]) - ord('0')) * 60\n",
    "           minCnt += (ord(str[3]) - ord('0')) * 10\n",
    "           minCnt += (ord(str[4]) - ord('0'))\n",
    "           return minCnt\n",
    "\n",
    "        timePointsMin = []\n",
    "        for tp in timePoints:\n",
    "            mins = convert(tp)\n",
    "            timePointsMin.append(mins)\n",
    "            timePointsMin.append(mins + DAY)\n",
    "\n",
    "        timePointsMin = sorted(timePointsMin)       \n",
    "        \n",
    "        minDiff = DAY + 1\n",
    "        for i in range(len(timePointsMin)-1):\n",
    "            diff = timePointsMin[i+1] - timePointsMin[i]\n",
    "            if diff < minDiff:\n",
    "                minDiff = diff\n",
    "        return minDiff"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "\n",
    "        def time_to_minute(s):\n",
    "            return int(s[:2]) * 60 + int(s[3:])\n",
    "\n",
    "        \n",
    "        minute_left = [time_to_minute(time) for time in timePoints]\n",
    "        minute_right = [time_to_minute(time) + 1440 for time in timePoints]\n",
    "        \n",
    "        minute = minute_left + minute_right\n",
    "        minute.sort()\n",
    "        res = float('inf')\n",
    "        for i in range(len(minute) - 1):\n",
    "            res = min(res, minute[i+1] - minute[i])\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        if len(timePoints) < 2:\n",
    "            return \n",
    "        timePoints = sorted(timePoints, key=lambda x:(int(x[:2]), int(x[3:])))\n",
    "\n",
    "        def sub_time(a, b, flag):\n",
    "            _a = [int(s) for s in a.split(':')]\n",
    "            _b = [int(s) for s in b.split(':')]\n",
    "\n",
    "            if flag:\n",
    "                return (_a[0]+24-_b[0])*60 + _a[1]-_b[1]\n",
    "            else:\n",
    "                return (_b[0]-_a[0])*60 + _b[1]-_a[1]\n",
    "\n",
    "        ans = sub_time(timePoints[0], timePoints[-1], True)\n",
    "      \n",
    "        for i in range(1, len(timePoints)):\n",
    "            ans = min(ans, sub_time(timePoints[i-1], timePoints[i], False))\n",
    "        return ans"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
